package com.wksc.framework.impexp.handler.exp;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.List;

import com.wksc.framework.impexp.constants.PostfixType;
import com.wksc.framework.impexp.template.ExpTemplate;
import com.wksc.framework.impexp.writer.TxtWriter;
import jxl.read.biff.BiffException;
import jxl.write.WriteException;

/**
 * <p> Title(文件名): ${file_name} </p>
 * <p> Description(描述): ${todo} </p>
 * <p> Copyright(版权): Copyright (c) ${year} </p>
 * <p> Company(公司): 成都四方信息技术有限公司 </p>
 * <p> CreateTime(生成日期)：11-11-7下午1:51 </p>
 *
 * @author wanglei
 * @version ${project_name}
 */
public class ExpTxtHandler extends BaseExpHandler {
	private static final long serialVersionUID = -2062305432196770257L;

	public static int number = 0;
	
	public ExpTxtHandler(ExpTemplate expTemplate) throws BiffException, FileNotFoundException, IOException {
		super(expTemplate);
	}

	@Override
	public int writeToFile(List<?> data) throws IOException, WriteException, BiffException {
		writer.initWriter(createFile(), true);
		int succNum = 0;
		try {
			/*1.写标题行*/
			if(number<1){
				writer.writeByRow(expTemplate.getShowedHeader(), 0);
			}
			/*2.写内容*/
			//如果模板中合并单元格属性不为空则使用合并单元格的方式写文件
			if (null != expTemplate.getExcelConflate()) {
				succNum = writer.writeByRows(data, 1);
			} else {
				// 写内容并返回
				succNum = writer.writeByRows(data, 1, expTemplate.getWriteOrder());
			}
		} finally {
			number++;
			writer.close();
		}
		return succNum;
	}

	@Override
	protected void init() throws BiffException, FileNotFoundException,
			IOException {
		writer = new TxtWriter();
	}

	@Override
	public PostfixType getPostfix() {
		return PostfixType.TXT;
	}
}
